package br.com.tcc.systemdx.web.editors;

import java.beans.PropertyEditorSupport;

import br.com.tcc.systemdx.domain.AtributoSintoma;
import br.com.tcc.systemdx.persistence.AtributoSintomaDAO;

public class AtributoSintomaPropertyEditor extends PropertyEditorSupport {
	
	private AtributoSintomaDAO atributoSintomaDAO;
	
	public AtributoSintomaPropertyEditor(AtributoSintomaDAO atributoSintomaDAO) {
		this.atributoSintomaDAO = atributoSintomaDAO;
	}
	
	@Override
	public String getAsText() {
		AtributoSintoma obj = (AtributoSintoma)getValue();
		if (obj == null)
			return "";
		else
			return obj.getId().toString();
	}
	
	@Override
	public void setAsText(final String value) {
		try {
			Long id = Long.parseLong(value);
			AtributoSintoma atributoSintoma = atributoSintomaDAO.findById(id);
			if (atributoSintoma != null)
				super.setValue(atributoSintoma);
			else
				throw new IllegalArgumentException("Binding error. Cannot find userAccount with id  ["+value+"]");
		} catch (NumberFormatException ex) {
			throw new IllegalArgumentException("Binding error. Invalid id: " + value);
		}
	}

}
